package org.pearl.springbootsecurity.demo.MP.data;

import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.io.ResolverUtil;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

/**
 * Created by TD on 2021/11/4
 * 类路径扫描器
 */
@Slf4j
public class ClassPathDataScopeScanner {

    static final String DEFAULT_RESOURCE_PATTERN = "**/*.class";

    static final String DEFAULT_CLASS_PATH = "classpath*:";


    /**
     * 扫描路径返回所有的class集合
     */
    public List<Class<?>> doScan(String packageName, Class<?> superType) {
        ResolverUtil<Class<?>> resolverUtil = new ResolverUtil<>();
        resolverUtil.find(new ResolverUtil.IsA(superType), packageName);
        Set<Class<? extends Class<?>>> mapperSet = resolverUtil.getClasses();
        return Arrays.asList(mapperSet.toArray(new Class<?>[0]));
    }

    /**
     * 扫描路径返回Resource集合
     */
    public Resource[] doScan(String packageName) throws IOException {
        PathMatchingResourcePatternResolver pathMatchingResourcePatternResolver = new PathMatchingResourcePatternResolver();
        String location = DEFAULT_CLASS_PATH + packageName + DEFAULT_RESOURCE_PATTERN;
        return pathMatchingResourcePatternResolver.getResources(location);
    }

}
